﻿// /////////////////////////////////// 
//  
//  
//      WiseLink CCF2.0 
//     (c)Copyright 2001-2010 WiseLink Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 

using System;
using System.Text;
using CCF20Model.Common.Infrastructure;
using System.Windows.Input;
using CCF20Model;
using System.Web.Profile;
using System.Web.Security;
using System.Windows;
using System.Diagnostics;
using System.IO;
using CCF20Model.ConfigurationFile;
using CCF20.Resources;
using WiseLink.RDS.Resource;

namespace CCF20Module
{
    class LoginWindowViewModel : ObservableObject
    {
        protected LoginWindowModel loginModel;

        public LoginWindowModel LoginModel
        {
            get { return loginModel; }
            set
            {
                loginModel = value;
                RaisePropertyChanged("LoginModel");
            }
        }
        public LoginWindowViewModel()
        {
            loginModel = new LoginWindowModel();
            loginModel.LoginStatus = ResultStatus.Succeed; //By default we don't require IDS login.
        }

        #region Commands

        public ICommand CCFLoginCommand
        {
            get { return new RelayCommand(CCFLoginExecute, CanCCFLoginExecute); }
        }

        public ICommand CCFChangePasswordCommand
        {
            get { return new RelayCommand(CCFChangePasswordExecute); }
        }
        public ICommand CCFSubmitPasswordCommand
        {
            get { return new RelayCommand(CCFSubmitPasswordExecute); }
        }
        public ICommand OpenConfigWindowCommand
        {
            get { return new RelayCommand(OpenConfigWindowExecute); }
        }
        public ICommand ExitCommand
        {
            get { return new RelayCommand(ExitExecute); }
        }

        #endregion //Commands

        void ExitExecute()
        {
            Close();
        }
        void OpenConfigWindowExecute()
        {
            Configuration config = new Configuration();
            config.Show();
        }

        void CCFChangePasswordExecute()
        {
            if (loginModel.ShowChangePwdPanel == System.Windows.Visibility.Collapsed)
            {
                loginModel.ShowChangePwdPanel = System.Windows.Visibility.Visible;
            }
            else
            {
                loginModel.ShowChangePwdPanel = System.Windows.Visibility.Collapsed;
            }
        }

        void CCFSubmitPasswordExecute()
        {
            if (Membership.ValidateUser(loginModel.CCFUserName, loginModel.CCFPassword))
            {
                MembershipUser mu = Membership.GetUser(loginModel.CCFUserName);
                if (mu.ChangePassword(loginModel.CCFPassword, loginModel.CCFChangedPassword))
                {

                    loginModel.CCFPassword = loginModel.CCFChangedPassword;
                    Utility.ShowMessageBox(string.Format("{0}{1}", loginModel.CCFUserName, StringLogin.lblPasswordChangeSucceed));
                    loginModel.ShowChangePwdPanel = System.Windows.Visibility.Collapsed;
                }
                else
                {
                    Utility.ShowMessageBox(string.Format("{0}{1}", loginModel.CCFUserName, StringLogin.lblPasswordChangeSucceed));
                }
            }
            else
            {
                Utility.ShowMessageBox(string.Format("{0}{1}", loginModel.CCFUserName, StringLogin.lblPasswordMismatch));
            }
        }

        
        Boolean CanCCFLoginExecute()
        {
            return loginModel.LoginStatus.Contains(ResultStatus.Succeed) ? true : false;
        }
        void CCFLoginExecute()
        {
            bool isValidUser = false;

            if (loginModel.CCFUserName.Equals("Admin", StringComparison.OrdinalIgnoreCase) && loginModel.CCFPassword.Equals("Admin", StringComparison.OrdinalIgnoreCase))
            {
                isValidUser = true;
            }
            if (Membership.ValidateUser(loginModel.CCFUserName, loginModel.CCFPassword))
            {
                isValidUser = true;
                StringBuilder sb = new StringBuilder();
                ModelFactory modelFactory = new ModelFactory();
                PermissionModel permissionModel = (PermissionModel)modelFactory.CreateModel(ModelName.PermissionModel);

                foreach (string role in Roles.GetRolesForUser(loginModel.CCFUserName))
                {

                    Application.Current.Properties["AllowedPermission"] += permissionModel.GetPermissionForRole(role);
                    LoggerHelper.LogCCF20(string.Format("User={0} Permission={1}", loginModel.CCFUserName, Application.Current.Properties["AllowedPermission"]));
                }
            }
            if (isValidUser)
            {
                Application.Current.Properties["CurrentUser"] = loginModel.CCFUserName;
                ProfileBase profile = ProfileBase.Create(loginModel.CCFUserName, true);
                Application.Current.Properties["CurrentUserMobile"] = profile.GetPropertyValue("Mobile").ToString();

                MainWindow mw = new MainWindow();
                ZICMonitorModel.UpdateZICUrl(ZICMonitorModel.ZICUrl, ZICMonitorModel.IsJava);
                mw.Show();
                
                if (!App.IsMobileBranch)
                {
                    LaunchZICMonitor();
                }
                Close();
            }
            else
            {
                Utility.ShowMessageBox(StringLogin.lblPasswordMismatch);
            }
        }
        private void LaunchZICMonitor()
        {
            Process[] runningProcs = Process.GetProcesses(".");
            foreach (Process p in runningProcs)
            {
                if (p.ProcessName.Contains("ZICMonitor"))
                {
                    p.Kill();
                }
            }

            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string myDir = path.Remove(path.LastIndexOf("\\"));
            DirectoryInfo dirInfo = new DirectoryInfo(myDir);
            FileInfo[] files = dirInfo.GetFiles("ZICMonitor.exe");
            if (files.GetLength(0) > 0)
            {
                Process.Start("ZICMonitor.exe");
            }
            else
            {
                Utility.ShowMessageBox(string.Format("{0}-{1}",StringLogin.lblMissingZICMonitor, myDir));
            }
        }
    }
}
